package com.guoyihao.codesandbox.codesandbox;


import com.guoyihao.codesandbox.model.ExecuteCodeRequest;
import com.guoyihao.codesandbox.model.ExecuteCodeResponse;
import java.util.Arrays;


/**
 * Java原生代码沙箱实现（模板使用）
 */
public class JavaNativeCodeSandbox extends JavaCodeSandBoxTemplate{
    /**
     * 测试
     * @param args
     */
    public static void main(String[] args) {
        String language = "java";
        //通过工厂实例化沙箱对象
        CodeSandBox codeSandbox = CodeSandBoxFactory.getCodeSandBox(language);
        //创建代码请求数据模型
        ExecuteCodeRequest executeCodeRequest = ExecuteCodeRequest.builder()
                .inputList(Arrays.asList(
                        //"2\n5\n6 -1 5 4 -7\n7\n0 6 -1 1 -6 7 -5\n"
                "3 4","1 2"))
                .code(
//                        "import java.util.Scanner;\n" +
////                        "import java.util.Arrays;\n" +
////                        "\n" +
////                        "public class Main {\n" +
////                        "    public static void main(String[] args) {\n" +
////                        "        Scanner scanner = new Scanner(System.in);\n" +
////                        "        \n" +
////                        "        int t = scanner.nextInt();  // 读取测试用例数量\n" +
////                        "        \n" +
////                        "        for (int testCase = 1; testCase <= t; testCase++) {\n" +
////                        "            int n = scanner.nextInt();  // 读取数组的大小\n" +
////                        "            int[] a = new int[n + 1];   // 数组下标从1开始\n" +
////                        "            \n" +
////                        "            // 读取数组元素\n" +
////                        "            for (int i = 1; i <= n; i++) {\n" +
////                        "                a[i] = scanner.nextInt();\n" +
////                        "            }\n" +
////                        "            \n" +
////                        "            // 动态规划求解最大子段和问题\n" +
////                        "            int maxSum = a[1];  // 初始最大和为第一个元素\n" +
////                        "            int currentStart = 1, left = 1, right = 1;\n" +
////                        "            int tempStart = 1;  // 临时起始下标\n" +
////                        "\n" +
////                        "            for (int i = 2; i <= n; i++) {\n" +
////                        "                if (a[i - 1] < 0) {\n" +
////                        "                    tempStart = i;  // 如果前面的和为负数，重置起始下标\n" +
////                        "                } else {\n" +
////                        "                    a[i] += a[i - 1];  // 累加前面的和\n" +
////                        "                }\n" +
////                        "\n" +
////                        "                // 更新最大和及左右边界\n" +
////                        "                if (a[i] > maxSum) {\n" +
////                        "                    maxSum = a[i];\n" +
////                        "                    left = tempStart;\n" +
////                        "                    right = i;\n" +
////                        "                }\n" +
////                        "            }\n" +
////                        "            \n" +
////                        "            // 输出结果\n" +
////                        "            System.out.println(\"Case \" + testCase + \":\");\n" +
////                        "            System.out.println(maxSum + \" \" + left + \" \" + right);\n" +
////                        "            if (testCase < t) {\n" +
////                        "                System.out.println();  // 多个测试用例之间空行\n" +
////                        "            }\n" +
////                        "        }\n" +
////                        "\n" +
////                        "        scanner.close();\n" +
////                        "    }\n" +
////                        "}\n"
                        "import java.util.*;\n" +
                                "public class Main {\n" +
                                "    public static void main(String[] args) {\n" +
                                "    Scanner scanner = new Scanner(System.in);\n" +
                                "        int a = scanner.nextInt();\n" +
                                "        int b = scanner.nextInt();\n" +
                                "        System.out.println(\"结果为：\" + (a + b));\n" +
                                "    }\n" +
                                "}")
                .language(language)
                .build();
//        String code = ResourceUtil.readStr("testCode/simpleComputeArgs/Main.java", StandardCharsets.UTF_8);
//        String code = ResourceUtil.readStr("testCode/unsafeCode/RunFileError.java", StandardCharsets.UTF_8);
//        String code = ResourceUtil.readStr("testCode/simpleCompute/Main.java", StandardCharsets.UTF_8);
        ExecuteCodeResponse executeCodeResponse = codeSandbox.executeCode(executeCodeRequest);
        System.out.println(executeCodeResponse);
    }

    @Override
    public ExecuteCodeResponse executeCode(ExecuteCodeRequest executeCodeRequest) {
        ExecuteCodeResponse executeCodeResponse = super.executeCode(executeCodeRequest);
        return executeCodeResponse;
    }
}



